const db=wx.cloud.database({})
const table=db.collection('Course')
const tb_courseSales=db.collection('CourseSales')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    class:"",
    endtime:0
  },

  onLoad: function (options) {
    let that=this
    that.setData({
  id:options.id
})
table.where({
  _id:this.data.id
}).get({
  success:res=>{
    let end=res.data[0].startTime+res.data[0].length
  that.setData({
    class:res.data,
    endtime:end
  })
  }
})
  },
  classOver:function(){
    let that=this;
  wx.showModal({
    title:'提示',
    content:'确认进行下课操作吗',
    success:function(res){
    tb_courseSales.where({
      courseId:that.data.id
    }).get({
      success:re=>{
        console.log(re)
        for(var i=0;i<re.data.length;i++){
          console.log(re.data[i].sign[re.data[i].status])
          if((re.data[i].sign[re.data[i].status]==3||re.data[i].sign[re.data[i].status]==1)&&re.data[i].remainingTimes-1!=0){console.log(1)
            tb_courseSales.doc(re.data[i]._id).update({
              data:{
              status:re.data[i].status+1,
              sign:db.command.push(3),
              remainingTimes:re.data[i].remainingTimes-1
              },
              success:function(res){console.log(res)}
            })
          }else if(re.data[i].sign[re.data[i].status]==2){
            
            var temp=re.data[i].sign;
            temp[temp.length-1]=3;
            console.log(temp)
            
            tb_courseSales.doc(re.data[i]._id).update({
              data:{
                sign:temp
              },
              success:function(res){console.log(res)}
            })
          }else{
            tb_courseSales.doc(re.data[i]._id).remove({
              success:function(res){
                console.log(res)
              }
            })
          }
        }
      }
    })
    }
  })
  }
})